const db = wx.cloud.database()
const getcardsCollection = db.collection('getcards')
const losecardsCollection = db.collection('losecards')
const getStuCollection = db.collection('getStu')
const loseStuCollection = db.collection('loseStu')

const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    fileList: [],
    show: true
  },

  //学生卡
  stunumber: function(event) {
    console.log(event.detail.value)
    this.setData({
      studentnumber: event.detail.value
    })
  },

  stuname: function(event) {
    console.log(event.detail.value)
    this.setData({
      studentname: event.detail.value
    })
  },

  stutelephone: function(event) {
    console.log(event.detail.value)
    this.setData({
      studenttelephone: event.detail.value
    })
  },

  stunotes: function(event) {
    console.log(event.detail.value)
    this.setData({
      studenttelephone: event.detail.value
    })
  },

  stusubmit: function(event){
    console.log("sub")
    getStuCollection.add({
      data:{
        studentnumber: this.data.studentnumber,
        studentname: this.data.studentname,
        studenttelephone: this.data.studenttelephone,
        studentnotes: this.data.studentnotes,
        state: false //false表示还没找到卡，true表示已经找到
      }
    }).then(res => {
      wx.showToast({
        title: '添加成功',
        icon: 'success',
        duration: 2000
      })
    })
    loseStuCollection.where({
      studentnumber: this.data.studentnumber
    }).field({
      _openid: true
    }).get().then(res => {
      if ((res.data).length != 0){
        this.setData({
          id: res.data
        })
        wx.cloud.callFunction({
          name:"sendmessage",
          data: {
            openid: this.data.id,
            category: '学生卡',
            telephone: this.data.studenttelephone,
          }
        })
      }
    })
  },

  // stusubmit: function(event) {
  //   getcardsCollection.add({
  //     data: {
  //       studentnumber: this.data.studentnumber,
  //       studentname: this.data.studentname,
  //       studenttelephone: this.data.studenttelephone,
  //       studentnotes: this.data.studentnotes,
  //       category: 1, //1:学生卡 2:身份证 3:银行卡
  //       state: false //false表示还没找到卡，true表示已经找到
  //     }
  //   }).then(res => {
  //     wx.showToast({
  //       title: '成功',
  //       icon: 'success',
  //       duration: 2000
  //     })
  //     losecardsCollection.where({
  //       studentnumber: this.data.studentnumber
  //     }).field({
  //       _openid: true
  //     }).get().then(res => {
  //       if ((res.data).length != 0) {
  //         console.log(res.data)
  //         this.setData({
  //           id: res.data
  //         })
  //         wx.cloud.callFunction({
  //           name: 'sendmessage',
  //           data: {
  //             openid: this.data.id,
  //             category: '学生卡',
  //             telephone: this.data.studenttelephone,
  //           }
  //         })
  //       }
  //     })
  //   })
  // },

  // 云函数
  // stusubmit: function(event){
  //   wx.cloud.callFunction({
  //     name:'add_stu',
  //     data:{
  //       studentnumber: this.data.studentnumber,
  //       studentname: this.data.studentname,
  //       studenttelephone: this.data.studenttelephone,
  //       studentnotes: this.data.studentnotes,
  //       category: 1,//1:学生卡 2:身份证 3:银行卡
  //       state: false //false表示还没找到卡，true表示已经找到
  //     }
  //   }).then(res => {
  //     console.log(res)
  //   })
  // },


  //小程序端
  // submit: function (event) {
  //   console.log(this.data.number)
  //   getcardsCollection.add({
  //     // data 字段表示需新增的 JSON 数据
  //     data: {
  //       number: this.data.number,
  //       name: this.data.name,
  //       telephone: this.data.telephone,
  //       category: false,//false表示还没找到卡，true表示已经找到
  //     }
  //   })
  //     .then(res => {
  //       losecardsCollection.field({
  //         number:true,
  //       }).where({
  //         number: this.data.number // 填入当前用户 openid
  //       }).get().then(res => {
  //         console.log(res.data)
  //         if(res.data.length != 0){
  //           this.setData({
  //             sendtelephone:res.data
  //           })
  //         }
  //       })
  //     })
  //     .catch(console.error)
  // },

  //云函数
  // submit: function(event){
  //   console.log("click")
  //   wx.cloud.callFunction({
  //     name:'addGetCard',
  //     data:{
  //       number: this.data.number,
  //       name: this.data.name,
  //       telephone: this.data.telephone,
  //       category: false,//false表示还没找到卡，true表示已经找到
  //     }
  //   }).then(res => {
  //     console.log(res)
  //   })
  // }


  //身份证
  identitynumber: function(event) {
    console.log(event.detail.value)
    this.setData({
      identitynumber: event.detail.value
    })
  },

  identityname: function(event) {
    console.log(event.detail.value)
    this.setData({
      identityname: event.detail.value
    })
  },

  identitytelephone: function(event) {
    console.log(event.detail.value)
    this.setData({
      identitytelephone: event.detail.value
    })
  },

  identitynotes: function(event) {
    console.log(event.detail.value)
    this.setData({
      identitynotes: event.detail.value
    })
  },

  // identitysubmit: function(event) {
  //   getcardsCollection.add({
  //     data: {
  //       identitynumber: this.data.identitynumber,
  //       identityname: this.data.identityname,
  //       identitytelephone: this.data.identitytelephone,
  //       identitynotes: this.data.identitynotes,
  //       category: 2, //1:学生卡 2:身份证 3:银行卡
  //       state: false //false表示还没找到卡，true表示已经找到
  //     }
  //   })
  // },

  identitysubmit: function(event) {
    getcardsCollection.add({
      data: {
        identitynumber: this.data.identitynumber,
        identityname: this.data.identityname,
        identitytelephone: this.data.identitytelephone,
        identitynotes: this.data.identitynotes,
        category: 2, //1:学生卡 2:身份证 3:银行卡
        state: false //false表示还没找到卡，true表示已经找到
      }
    }).then(res => {
      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 2000
      })
      losecardsCollection.where({
        identitynumber: this.data.identitynumber
      }).field({
        _openid: true
      }).get().then(res => {
        console.log(res.data)
        if ((res.data).length != 0) {
          console.log(res.data)
          this.setData({
            id: res.data
          })
          wx.cloud.callFunction({
            name: 'sendmessage',
            data: {
              openid: this.data.id,
              category: '身份证',
              telephone: this.data.identitytelephone,
            }
          })
        }
      })
    })
  },

  //银行卡
  banknumber: function(event) {
    console.log(event.detail.value)
    this.setData({
      banknumber: event.detail.value
    })
  },

  bankname: function(event) {
    console.log(event.detail.value)
    this.setData({
      bankname: event.detail.value
    })
  },

  banktelephone: function(event) {
    console.log(event.detail.value)
    this.setData({
      banktelephone: event.detail.value
    })
  },

  banknotes: function(event) {
    console.log(event.detail.value)
    this.setData({
      banknotes: event.detail.value
    })
  },

  // banksubmit: function(event) {
  //   getcardsCollection.add({
  //     data: {
  //       banknumber: this.data.banknumber,
  //       banktelephone: this.data.banktelephone,
  //       banknotes: this.data.banknotes,
  //       category: 3, //1:学生卡 2:身份证 3:银行卡
  //       state: false //false表示还没找到卡，true表示已经找到
  //     }
  //   })
  // },

  banksubmit: function(event) {
    getcardsCollection.add({
      data: {
        banknumber: this.data.banknumber,
        bankname: this.data.bankname,
        banktelephone: this.data.banktelephone,
        banknotes: this.data.banknotes,
        category: 3, //1:学生卡 2:身份证 3:银行卡
        state: false //false表示还没找到卡，true表示已经找到
      }
    }).then(res => {
      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 2000
      })
      losecardsCollection.where({
        banknumber: this.data.banknumber
      }).field({
        _openid: true
      }).get().then(res => {
        console.log(res.data)
        if ((res.data).length != 0) {
          console.log(res.data)
          this.setData({
            id: res.data
          })
          wx.cloud.callFunction({
            name: 'sendmessage',
            data: {
              openid: this.data.id,
              category: '银行卡',
              telephone: this.data.banktelephone,
            }
          })
        }
      })
    })
  },



})